<?php
/**
 * Miffos framework
 * ----------------------------------------------
 *
 * Copyright (c) 2008, 2009 Martin Sadový (http://sodae.cz)
 *
 * This source file is subject to the "Miffos license" that is bundled
 * with this package in the file license.txt.
 *
 * @copyright  Copyright (c) 2008, 2009 Martin Sadový
 * @license    license.txt
 * @link       http://miffos.sodae.cz
 * @package    miffos
 * @version    $Id$
 */
  require_once dirname(__FILE__).'/../Html.php';

  class TableHelper {
  
    private $html;
    
    private $addrow = false;
    
    
    public function __construct($attrs = null)
    { 
      $this->html = Html::el("table",$attrs);
    }  
    
    public function sethead()
    {
      $getArgs = func_get_args();
      $numArgs = func_num_args();
      return $this->_parseInputRow("th",$getArgs,$numArgs);
    }
    
    
    public function addRow()
    {
      $getArgs = func_get_args();
      $numArgs = func_num_args();
      return $this->_parseInputRow("td",$getArgs,$numArgs);
    }
    
    
    private function _parseInputRow($type,$param,$numParam)
    {
      if($numParam == 0){
      /** throw new Exception("Must write one name column"); **/
      }elseif($numParam == 1){
        if(is_array($param[0])){
          return $this->_parseRow($type,$param[0]);
        }else{
          return $this->_parseRow($type,array($param[0]));
        }
      }else{
        return $this->_parseRow($type,$param);
      }
    }
    
    private function _parseRow($type,array $rows)
    {
      $this->addrow = true;
      $tr = $this->html->create("tr");
      foreach($rows as $row){
        $tr->create($type)->setHtml($row);
      }
      return $tr;
    }
    
    public function addCaption($value)
    {
      $this->html->create("caption",$value);
    }
    
    public function render()
    {
      return $this->html->render();
    }
  
    
    public function __toString()
    {
      return $this->render();
    }
  
  }

?>